package com.zkk.dao.impl;

import com.zkk.dao.CardDao;
import com.zkk.pojo.Card;
import com.zkk.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.util.List;

public class CardDaoImpl implements CardDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());
    @Override
    public int addCard(Card card) {
        int update = jdbcTemplate.update("insert into card(username,id_card,telephone) values (?,?,?)", card.getUsername(), card.getId_card(), card.getTelephone());
        return update;
    }

    @Override
    public List<Card> showAccount() {
        List<Card> query = jdbcTemplate.query("select * from card", new BeanPropertyRowMapper<>(Card.class));
        return query;
    }

    @Override
    public Card queryMoney(Card card) {
        List<Card> query = jdbcTemplate.query("select * from card where id_card=?", new BeanPropertyRowMapper<>(Card.class),card.getId_card());
        if(query.size()==1 & query !=null){
            return query.get(0);

        }
        return null;
    }

    @Override
    public int rechargingMoney(String id_card, BigDecimal totalMoney) {
        int update = jdbcTemplate.update("update card set balance = ? where id_card = ?", totalMoney, id_card);
        return update;
    }

}
